package org.itboys.mobile.service.mongo.web.member;

import org.apache.commons.lang.StringUtils;
import org.itboys.commons.utils.encryption.Digests;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.mdl.conmon.Finals;
import org.itboys.mobile.entity.mongo.SundayMember;
import org.itboys.mobile.util.Md5UtilJx;
import org.itboys.mobile.util.NickNameUtil;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2017/12/4
 * 描述：迈迪龙_用户
 */
@Service
public class SundayMemberService extends BaseMongoService<SundayMember>{
    @Resource(name="mdlDs")
    private MongoDataSource mongoDataSource;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return mongoDataSource;
    }
    @Override
    protected Class<SundayMember> getEntityClass() {
        return SundayMember.class;
    }

    public void saveMember(SundayMember member){
        if(member.getId()==0l){
            member.setPassword(StringUtils.isNotEmpty(member.getPassword())? Digests.md5(member.getPassword()):Digests.md5(Finals.DEFAULT_PASSWORD));
            member.setImage(StringUtils.isNotEmpty(member.getImage())?member.getImage(): NickNameUtil.getRandomLogo());
            member.setRecommenderId(0L);
            member.setAgentId(0l);
            //默认一些变量
            //member.setIsSeller(0);
          /*  member.setIsWeixinLogin(0);
            member.setIsQqLogin(0);
            member.setIsWeiboLogin(0);
            member.setRecommenderId(0L);
            member.setSellerId(0l);
            member.setDistributorId(0L);
            member.setAgentId(0l);*/

            super.save(member);
        }else{
            super.updateExceptEmpty(member.getId(), member);
        }
    }

    /**
     * 查询
     * @param request
     * @param userTypes
     * @return
     */
    public PageResult<SundayMember> selectMember(HttpServletRequest request, List<Integer> userTypes,Long agentId){
        Map<String,Object> param= ParamUtil.packageMongoExactParam(request,"userName","userType");
        param.put("isDeleted",Finals.NO);
        if(ListUtil.isNotNull(userTypes)){
            param.put("userType in", userTypes);
        }
        if(LongUtil.isNotZreo(agentId)){
            param.put("agentId",agentId);
        }
        Map<String,String> containsparam =ParamUtil.packageMongoVagueParam(request,"nickName","provinceName","cityName","districtName","townName");
        return super.containMemberQuery(request,containsparam,param);


    }

    /**
     * 查找单个
      * @param memberId
     * @return
     */
    public SundayMember findOne(Long memberId){
        SundayMember member=null;
        if(LongUtil.isNotZreo(memberId)){
            member=super.getById(memberId);
        }else {
            member=new SundayMember();
        }
        return member;
    }

    /**
     * 重置密码
     * @param memberId
     */
    public void resetPassword(Long memberId){
        super.update(memberId, "password", Digests.md5(Finals.DEFAULT_PASSWORD));
    }

    /**
     * 后台审核销售员
     * @param salesManId
     * @param isHandle
     */
    public void handleSalesMan(Long salesManId,int isHandle){
        SundayMember salesMan=super.getById(salesManId);
        salesMan.setSalesManStatus(isHandle);
        super.updateExceptEmpty(salesManId,salesMan);
    }

    /**
     * 冻结或者启用  销售员和维修工
     * @param memberId
     * @param status
     */
    public void switchStatus(Long memberId,int status){
        SundayMember member=super.getById(memberId);
        member.setStatus(status);
        super.updateExceptEmpty(memberId,member);
    }
    /**
     * 删除
     * @param memberId
     */
    public void deleteMember(Long memberId){
        super.delete(memberId);
        logger.info("删除用户","id="+memberId);
    }

}
